package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDiagnosisDetail;
import com.ruoyi.system.service.IHomePageService;
import com.ruoyi.system.service.ISysDiagnosisDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.ExcelResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwq
 * @ClassName HomePageController.java
 * @Description //TODO 首页数据统计$
 * @createTime 2022-06-23 16:25
 */
@Api(tags = "首页数据统计")
@RestController
@RequestMapping("/system/home")
public class HomePageController extends BaseController{
    @Autowired
    private IHomePageService HomePageService;

    /** 试卷导出模板路径 */
    public static final String EXAM_WORD_TEMPLATE_PATH = "templates/word/digital_test.docx";

    /**
     * 查询首页统计数据
     * @return
     */
    @ApiOperation(value = "查询首页统计数据")
    @GetMapping("/page")
    public AjaxResult selectHomePageVo()
    {

        return AjaxResult.success(HomePageService.selectHomePageVo());
    }


    /**
     * 导出试卷word
     *
     * @param response
     * @param examId
     * @return
     * @throws IOException
     */
 //   @ApiOperation(value = "导出试卷word")
    @GetMapping("/digital/test/word")
    public void exportExamUserDtoWord(HttpServletResponse response) throws IOException {
        Configure config = Configure.builder()
                // isStrict = false , 关闭严格模式, 严格模式下key不存在会报错
                // 关闭后key不存在会被忽略
               // .useSpringEL(true)
                // 表格绑定policy
                .build();



        ClassPathResource classPathResource = new ClassPathResource(EXAM_WORD_TEMPLATE_PATH);

        // 字体Wingdings 2的符号
        TextRenderData selSymbol = new TextRenderData("R",new Style("Wingdings 2",10));//勾选框

        TextRenderData unselSymbol = new TextRenderData("\u00A3",new Style("Wingdings 2",10));//空框

        Map<String, Object> map=new HashMap<String, Object>() {{
            put("1-1", selSymbol);
            put("1-2", unselSymbol);
            put("1_3", selSymbol);
            put("t1_4", selSymbol);
            put("tag1", selSymbol);

            put("2-1", selSymbol);
            put("2-2", selSymbol);
            put("2-3", selSymbol);
            put("2-4", selSymbol);

        }};
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream(),config).render(map);
        OutputStream os = response.getOutputStream();
        // 响应到客户端
        ExcelResponse.disposition(response, "测评表" + ".docx", "attachment", 0);
        template.write(os);
        os.flush();
        os.close();
        if (template != null) {
            template.close();
        }


    }
}
